<?php
//load common configuration
Loader::loadClass('SubdomainCreator','modules/SubdomainManagement/pnincludes');

function SubdomainManagement_admin_createClient($args)
{
  
  $args = json_decode(file_get_contents('php://input'));

  
  //echo "Request : <br>".print_r($args); 
  if($args->shop_subdomain){
    $obj = new SubdomainCreator(NULL, NULL, NULL, NULL, 'iShop');
    

    //Mock create DB
    /*  $otherTable = array(
                          "ishopstore_information" => array(
                                                              'uid'               => 2,
                                                              'firstname'         => 'firstname',
                                                              'lastname'          => 'lastname',
                                                              'mobilephone'       => 'mobilephone',
                                                              'gender'            => 'gender',
                                                              'house_no'          => 'house_no',                                        
                                                              'moo'               => 'moo',                                           
                                                              'soi'               => 'soi',                                         
                                                              'road'              => 'road',                                         
                                                              'village'           => 'village', 
                                                              'tambon'            => 'tambon', 
                                                              'district'          => 'district',                                                                                                                         
                                                              'province'          => 'province', 
                                                              'zipcode'           => 'zipcode',                                                                                 
                                                              'shop_subdomain'    => 'shop_subdomain',
                                                              'shop_domain'       => 'shop_domain',
                                                              'shop_name'         => 'shop_name',
                                                              'shop_database'     => 'shop_database',
                                                              'shop_description'  => 'shop_description',                                                                                
                                                              'shop_icon'         => 'shop_icon',                                                                                
                                                              'shop_theme'        => 'shop_theme'  
                          
                                                      )
                        
                  );    
    
      $isCreate = $obj->makedb('okalimar_test01', 'okalimar', '142c5d3b017089bb147f1129806480f11777897a49327f74657b9db59b899259', 'test0@gmail.com', 'iShop', 'okalimart.com', 'test01', $otherTable); 
*/
    //Create data
    
    $otherTable = array(
                          "ishopstore_information" => array(
                                                              'uid'               => $args->uid,
                                                              'firstname'         => $args->firstname,
                                                              'lastname'          => $args->lastname,
                                                              'mobilephone'       => $args->mobilephone,
                                                              'gender'            => $args->gender,
                                                              'house_no'          => $args->house_no,                                        
                                                              'moo'               => $args->moo,                                           
                                                              'soi'               => $args->soi,                                         
                                                              'road'              => $args->road,                                         
                                                              'village'           => $args->village, 
                                                              'tambon'            => $args->tambon, 
                                                              'district'          => $args->district,                                                                                                                         
                                                              'province'          => $args->province, 
                                                              'zipcode'           => $args->zipcode,                                                                                 
                                                              'shop_subdomain'    => $args->shop_subdomain,
                                                              'shop_domain'       => $args->shop_domain,
                                                              'shop_name'         => $args->shop_name,
                                                              'shop_database'     => $args->shop_database,
                                                              'shop_description'  => $args->shop_description,                                                                                
                                                              'shop_icon'         => $args->shop_icon,                                                                                
                                                              'shop_theme'        => $args->shop_theme  
                          
                                                      )
                        
                  );
                   
    $isCreate = $obj->makedb($args->dbname, $args->uname, $args->pass, $args->email, $args->shop_theme, $args->hostname, $args->shop_subdomain, $otherTable);
		
		
    $response = array();
    if($isCreate){
      $response = array(
                        "response" => "success",
                        "domain"   => $args->shop_subdomain,
                        "error"    => "none"                                               
                  );
    }else{
      $response = array("response" => "failed",
                        "domain"   => $args->shop_subdomain,      
                        "error"    => "Duplicate database & subdomain"
                  );    
    }
 
  }else{  
      $response = array("response" => "failed",
                        "domain"   => $args->shop_subdomain,      
                        "error"    => "No subdomain argument"
                  );      
  }
  echo json_encode($response);
  pnShutDown();
}

?>